package com.atguigu.tingshu.user.strategy.impl;

import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Package: com.atguigu.tingshu.user.strategy.impl
 *
 * @author xiongkunYang
 * @description: 处理购买项目类型 ItemType 为VIP会员的情况
 * @since 2025/6/26 10:11
 */
@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)
public class VipStrategyImpl implements ItemTypeStrategy {

    private final UserInfoMapper userInfoMapper;
    private final VipServiceConfigMapper vipServiceConfigMapper;
    private final UserVipServiceMapper userVipServiceMapper;

    public VipStrategyImpl(UserInfoMapper userInfoMapper, VipServiceConfigMapper vipServiceConfigMapper, UserVipServiceMapper userVipServiceMapper) {
        this.userInfoMapper = userInfoMapper;
        this.vipServiceConfigMapper = vipServiceConfigMapper;
        this.userVipServiceMapper = userVipServiceMapper;
    }

    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("处理购买项目类型为：VIP会员");
        String orderNo = userPaidRecordVo.getOrderNo();
        Long count = this.userVipServiceMapper.selectCount(new LambdaQueryWrapper<UserVipService>()
                .eq(UserVipService::getOrderNo, orderNo));
        if (count > 0) {
            log.error("同一订单不可重复支付！");
            return;
        }
        Long userId = userPaidRecordVo.getUserId();
        // 创建用户 vip 购买记录对象，更新用户 VIP 服务记录（user_vip_service）
        UserVipService userVipService = new UserVipService();
        userVipService.setOrderNo(orderNo);
        userVipService.setUserId(userId);

        // 查询 vip 服务配置信息，获取 vip 会员月数
        Long vipConfigId = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig vipServiceConfig = this.vipServiceConfigMapper.selectById(vipConfigId);
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        // 查询用户信息，判断用户是否为会员
        UserInfo userInfo = this.userInfoMapper.selectById(userId);
        Date vipExpireTime = userInfo.getVipExpireTime();
        boolean isVip = userInfo.getIsVip() == 1 && vipExpireTime != null && vipExpireTime.after(new Date());

        // 定义 vip 服务生效时间，如果用户是会员，则设置 vip 服务生效时间为会员的到期时间，否则设置为当前时间
        Date startTime = isVip ? vipExpireTime : new Date();
        // 定义 vip 服务到期时间
        Date expireTime = DateUtil.offsetMonth(startTime, serviceMonth);
        // 封装 vip 服务的生效时间和到期时间
        userVipService.setStartTime(startTime);
        userVipService.setExpireTime(expireTime);
        // 定义 vip 服务是否自动续费
        userVipService.setIsAutoRenew(0);
        // 插入用户 vip 购买记录
        this.userVipServiceMapper.insert(userVipService);

        // 更新用户信息（user_info）
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        this.userInfoMapper.updateById(userInfo);

    }
}